package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysDeptService;

import com.ruoyi.system.domain.vo.SjOrderHistoryVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SjOrderHistory;
import com.ruoyi.system.service.ISjOrderHistoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 整改下发Controller
 * 
 * @author zrr
 * @date 2024-11-04
 */
@RestController
@RequestMapping("/orderHistory/orderHistory")
public class SjOrderHistoryController extends BaseController
{
    @Autowired
    private ISjOrderHistoryService sjOrderHistoryService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询整改下发列表
     */
    @PreAuthorize("@ss.hasPermi('orderHistory:orderHistory:list')")
    @GetMapping("/list")
    public TableDataInfo list(SjOrderHistory sjOrderHistory)
    {
        startPage();
        if(sjOrderHistory.getCreateTime() != null){
            sjOrderHistory.setCreateMonth(DateUtils.parseDateToStr("yyyy-MM",sjOrderHistory.getCreateTime())+"%");
        }
        List<SjOrderHistory> list = sjOrderHistoryService.selectSjOrderHistoryList(sjOrderHistory);
        return getDataTable(list);
    }

    /**
     * 导出整改下发列表
     */
    @PreAuthorize("@ss.hasPermi('orderHistory:orderHistory:export')")
    @Log(title = "整改下发", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SjOrderHistory sjOrderHistory)
    {
        List<SjOrderHistory> list = sjOrderHistoryService.selectSjOrderHistoryList(sjOrderHistory);
        ExcelUtil<SjOrderHistory> util = new ExcelUtil<SjOrderHistory>(SjOrderHistory.class);
        util.exportExcel(response, list, "整改下发数据");
    }

    /**
     * 获取整改下发详细信息
     */
    @PreAuthorize("@ss.hasPermi('orderHistory:orderHistory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(sjOrderHistoryService.selectSjOrderHistoryById(id));
    }

    /**
     * 新增整改下发
     */
    @PreAuthorize("@ss.hasPermi('orderHistory:orderHistory:add')")
    @Log(title = "整改下发", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SjOrderHistory sjOrderHistory)
    {
        if(!sjOrderHistory.getDepartmentCode().isEmpty()){
            SysDept sd = deptService.selectDeptById(Long.parseLong(sjOrderHistory.getDepartmentCode()));
            sjOrderHistory.setDepartment(sd.getDeptName());
        }
        return toAjax(sjOrderHistoryService.insertSjOrderHistory(sjOrderHistory));
    }

    /**
     * 修改整改下发
     */
    @PreAuthorize("@ss.hasPermi('orderHistory:orderHistory:edit')")
    @Log(title = "整改下发", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SjOrderHistory sjOrderHistory)
    {
        if(!sjOrderHistory.getDepartmentCode().isEmpty()){
            SysDept sd = deptService.selectDeptById(Long.parseLong(sjOrderHistory.getDepartmentCode()));
            sjOrderHistory.setDepartment(sd.getDeptName());
        }
        return toAjax(sjOrderHistoryService.updateSjOrderHistory(sjOrderHistory));
    }

    /**
     * 删除整改下发
     */
    @PreAuthorize("@ss.hasPermi('orderHistory:orderHistory:remove')")
    @Log(title = "整改下发", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(sjOrderHistoryService.deleteSjOrderHistoryByIds(ids));
    }

    /**
     * 部门异常分布数量
     */
    @PreAuthorize("@ss.hasPermi('orderHistory:orderHistory:exceptionType')")
    @GetMapping(value = "/exceptionTypeCount")
    public List<SjOrderHistoryVO> exceptionTypeCount(){
        return sjOrderHistoryService.selectSjOrderHistoryCount();
    }
}
